﻿using EntitySpaces.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Fin_Fin007b : AdminBasePage
{
    decimal lendID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VLendBoxMetadata.ColumnNames.LendID] != null)
        {
            lendID = decimal.Parse(Request.QueryString[VLendBoxMetadata.ColumnNames.LendID]);
        }

    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    /// <summary>
    /// 只加载状态为"存储中",location 跟shipping from location一致,box状态为close而且不是转移中的box
    /// </summary>
    /// <param name="page"></param>
    void GridDataBind(int page)
    {
        if (string.IsNullOrEmpty(txtBoxNo.Text) && string.IsNullOrEmpty(txtDocNo.Text))
        {
            lbMessage.Text = "请输入凭证盒号或凭证索引号！";
            return;
        }
        FinBoxReceiptsQuery q1 = new FinBoxReceiptsQuery("Q1");
        FinBoxQuery q2 = new FinBoxQuery("Q2");
        FinReceiptQuery q3 = new FinReceiptQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");

        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.InnerJoin(q3).On(q1.ReceiptID == q3.ReceiptID);
        q1.LeftJoin(q4).On(q3.DocumentsType == q4.Code);

        q1.Where(q3.StorageStatus == ConstantValue.StorageStatusCode.InStock && q3.StorageLocation == LoginUserInfo.CurrentUser.ThirdUnitID);

        if (txtBoxNo.Text.Trim().Length > 0)
        {
            q1.Where(q2.BoxNo.Like("%" + txtBoxNo.Text + "%"));
        }
        if (txtDocNo.Text.Trim().Length > 0)
        {
            q1.Where(q3.DocumentsNo.Like("%" + txtDocNo.Text + "%") || q3.Reference.Like("%" + txtDocNo.Text + "%") || q3.ReferenceKey.Like("%" + txtDocNo.Text + "%"));
        }
        q1.Select(q2.BoxNo, q3.DocumentsNo, q3.DocumentsType, q3.Reference, q3.ReferenceKey, q4.Value1.As("DocTypeName"),q3.ReceiptID,q1.BoxID);
        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        GridDataBind(1);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            FinReceipt finReceipt = new FinReceipt();

            FinLendReceipt lendReceipt = new FinLendReceipt();
            FinBox finBox = new FinBox();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbRept = (Label)item.FindControl("lbReptID");
                        lendReceipt = new FinLendReceipt();
                        lendReceipt.ReceiptID = decimal.Parse(lbRept.Text);
                        lendReceipt.LendID = lendID;
                        lendReceipt.UpdatedBy = lendReceipt.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        lendReceipt.UpdatedDttm = lendReceipt.CreatedDttm = DateTime.Now;
                        lendReceipt.Save();

                        //添加后将凭证状态设置为借出
                        finReceipt.LoadByPrimaryKey(lendReceipt.ReceiptID.Value);
                        finReceipt.StorageStatus = ConstantValue.StorageStatusCode.Loan;
                        finReceipt.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        finReceipt.UpdatedDttm =  DateTime.Now;
                        finReceipt.Save();

                        Label lbBox = (Label)item.FindControl("lbBoxID");

                        finBox.LoadByPrimaryKey(decimal.Parse(lbBox.Text));
                        finBox.StorageStatus = ConstantValue.StorageStatusCode.Loan;
                        finBox.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        finBox.UpdatedDttm = DateTime.Now;
                        finBox.Save();
                        
                    }
                }
                tr.Complete();

                WebHelper.CloseDialog(true);

            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}